package com.chat.data;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * MySql单例构造器
 * 采用单例模式
 * @author XieYangLin
 *
 */
public class MySqlSingle {
    private static MySqlSingle mySqlSingle;
    private SqlSessionFactory sqlSessionFactory;


    private MySqlSingle() throws IOException {
        String resource = "com/chat/mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

    }

    public static MySqlSingle getMySqlSingle() throws IOException {
        if(mySqlSingle == null)
            return new MySqlSingle();
        return mySqlSingle;
    }
    //获得SessionFactory
    public SqlSessionFactory getSqlSessionFactory() {
        return sqlSessionFactory;
    }
}
